package bigoo.peer;

import bigoo.peer.PublisherSource.RideRequest;
import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.Sim_normal_obj;

public class SearcherSource extends Sim_entity {

    private final Sim_port out;
    private final Sim_normal_obj delayGenerator;

    public SearcherSource(String peerID, double mean) {
	super(peerID + "Searcher");
	this.out = new Sim_port("out");
	this.add_port(this.out);
	this.delayGenerator = new Sim_normal_obj("SearcherDelay", mean, 1);
    }

    @Override
    public void body() {
	while (Sim_system.running()) {
	    this.sim_pause(Math.abs(this.delayGenerator.sample()));
	    if (PublisherSource.rides.size() > 0) {
		RideRequest ride = PublisherSource.rides.get((int) (Math.random() * PublisherSource.rides.size()));

		this.sim_schedule(this.out, 0.0, BigooPeer.LOCAL_SEARCH_EVENT, ride);
	    }
	}
    }
}
